﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ElevatorLib
{
    /// <summary>
    /// LogNormal分布
    /// </summary>
    public class LogNormalDistr : ElevatorLib.IDistribution
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="rnd"></param>
        /// <param name="mean"></param>
        /// <param name="dev"></param>
        public LogNormalDistr(Random rnd, double mean, double dev)
        {
            if(mean <= 0 || dev <= 0.0)
            {
                mean = 0;
                dev = 0;
                throw new Exception("Mean and dev must be greater than 0,0");
            }
            this.rnd = rnd;
            this.mean = mean;
            this.dev = dev;
        }

        #region Properties
        /// <summary>
        /// 随机数
        /// </summary>
        private Random rnd;
        /// <summary>
        /// 大于0
        /// </summary>
        private double mean;
        /// <summary>
        /// 大于0
        /// </summary>
        private double dev;
        #endregion
        #region IDistribution 成员
        /// <summary>
        /// 生成数据
        /// </summary>
        /// <returns></returns>
        public double Sample()
        {
            double n = Math.Sqrt(-2 * Math.Log(Math.E, rnd.NextDouble())) * Math.Sin(2 * Math.PI * rnd.NextDouble());

            return Math.Pow(Math.E, mean + dev * n);
        }

        #endregion
    }
}
